



//
//  SHTradingQuryController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/10/23.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "CYFDateView.h"//时间选择器
#import "XDActionSheet.h"//底部弹出菜单
#import "SHTradingQueryDateHeader.h"//头部时间选择
#import "SHChangeFooter.h"//底部查询按钮
#import "SHChangeCardHeaderView.h"//tableView header
#import "SHTradingQuryBaseCell.h"//交易方式 交易状态cell
#import "TradeNotesViewController.h"//交易记录列表
#import "SHTradingQuryController.h"

@interface SHTradingQuryController ()<UITableViewDelegate,UITableViewDataSource,SHChangeFooterDelegate,XDActionSheetDelegate,XDActionSheetDatasource,SHTradingQueryDateHeaderDelegate,CYFDateViewDelegate>
//主列表
@property(nonatomic,strong)UITableView *tradingQuryTableView;

//开始时间
@property(nonatomic,strong)NSString *benginDate;

//结束时间
@property(nonatomic,strong)NSString *endDate;

//交易方式
@property(nonatomic,strong)NSString *paymentTypeStr;

//交易方式类型
@property(nonatomic,assign)NSInteger paymentType;

//交易状态
@property(nonatomic,strong)NSString *paymentStatu;

//

@property(nonatomic,strong)SHTradingQueryDateHeader *queryDataHeader;
//交易方式列列表
@property(nonatomic,strong)XDActionSheet *paymentTypeSheet;

//交易状态列列表
@property(nonatomic,strong)XDActionSheet *paymentStatuSheet;

//交易方式标题数组
@property(nonatomic,strong)NSArray *paymentTypeTitleArray;

//交易状态标题数组
@property(nonatomic,strong)NSArray *paymentStatuTitleArray;

//时间选择器
@property(nonatomic,strong)CYFDateView *dateView;//时间选择器




@end

@implementation SHTradingQuryController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title  = @"交易查询";
    self.view.backgroundColor = [UIColor whiteColor];
    [self getPayChoiceCashType];
    //初始化默认数据
    [self initPaymentTypeAndPaymentStatu];
    //初始化列表
    [self tradingQuryTableView];
    //创建头部时间
   self.queryDataHeader = [[SHTradingQueryDateHeader alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(90))];
    self.queryDataHeader.delegate = self;
    self.tradingQuryTableView.tableHeaderView = self.queryDataHeader;
    
    
    // Do any additional setup after loading the view.
}

#pragma mark - 查询可用银联支付类型
- (void)getPayChoiceCashType{
    
    [SHHomeModelView getPayChoiceCashTypeSuccess:^(NSInteger cashType) {
        switch (cashType) {
            case 0://瀚银银联收款
                
                self.paymentType = 6;
                break;
            case 1://杉奇银联收款
                
                self.paymentType = 5;

                break;
            case 2://上福银联收款
                self.paymentType = 8;
                break;

            case 3://富汇通银联收款
                self.paymentType = 7;
    
                break;
                
            default:
                break;
        }
        
        
    }];
    
}

#pragma mark - 设置默认数据
- (void)initPaymentTypeAndPaymentStatu{
    
    self.paymentTypeStr = @"我是瀚付";
    self.paymentStatu = @"全部";
    self.paymentTypeTitleArray = @[@"我是瀚付",@"杉德收款",@"微信扫码支付",@"支付宝扫码支付"];
    self.paymentStatuTitleArray = @[@"全部",@"支付成功",@"等待支付",@"支付失败"];
    self.benginDate = [Tool getCurrentDate];
    self.endDate = [Tool getCurrentDate];

}
#pragma  mark - 懒加载
- (UITableView *)tradingQuryTableView{
    
    if (!_tradingQuryTableView) {
        
        _tradingQuryTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT ) style:UITableViewStylePlain];
        _tradingQuryTableView.backgroundColor = KBASEBGCOLOR;
        _tradingQuryTableView.delegate = self;
        _tradingQuryTableView.dataSource = self;
        [self.view addSubview:_tradingQuryTableView];
        _tradingQuryTableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
        [_tradingQuryTableView registerClass: [SHTradingQuryBaseCell  class]  forCellReuseIdentifier:@"PaymentwayCell"];
        [_tradingQuryTableView registerClass: [SHChangeFooter  class]  forCellReuseIdentifier:@"SHChangeFooter"];

        
      
        
    }
    return _tradingQuryTableView;
}



#pragma mark --- UITablView  & DataSource  & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
}

#pragma mark - 3行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
        
        return  3;

   
    
}

#pragma mark - 头部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return  __I6Height(15);
}




#pragma mark - cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
        if (indexPath.row == 0 || indexPath.row == 1) {
            
            return __I6Height(44);
            
        }else{
            
            return __I6Height(142);
        }
    
    
}

#pragma mark - 头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
        
        SHChangeCardHeaderView *header = [[SHChangeCardHeaderView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth,__I6Height(20))];
        
        
        
        
        return  header;
    
   

}




#pragma mark - cell 数据源
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

        if(indexPath.row == 0 || indexPath.row == 1){
            
            SHTradingQuryBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PaymentwayCell"];
            if (indexPath.row == 0) {
                cell.leftName = @"交易方式";
                cell.contentName = self.paymentTypeStr;
                
            }else{
                cell.leftName = @"交易状态";
                cell.contentName = self.paymentStatu;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return  cell;
            
        }else{
            
            SHChangeFooter *  footer = [tableView dequeueReusableCellWithIdentifier:@"SHChangeFooter"];
            footer.buttonTitle = @"查询";
            footer.delegate = self;
            footer.isClickedBtn = YES;
            footer.selectionStyle = UITableViewCellSelectionStyleNone;

            return footer;
            
            
        }
        
       

    
}




#pragma mark - 点击列表  交易方式  交易状态
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    switch (indexPath.row) {
        case 0://切换交易方式
        {
            [self.paymentTypeSheet actionSheetShow:self.paymentTypeTitleArray.count + 1];

        }
            break;
        case 1://切换交易状态
        {
             [self.paymentStatuSheet actionSheetShow:self.paymentStatuTitleArray.count  + 1];
        }
            break;
        default:
            break;
    }
        
        
    
    
    
    
}

#pragma mark - 时间 弹出时间选择器
- (void)tradingQueryDateHeader:(UIView *)header dateControl:(SHTradingDateControl *)control tag:(int)tag{
    
    self.dateView = [[CYFDateView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.dateView.delegate = self;
    self.dateView.dateTag = tag;
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.dateView];
    [self.view endEditing:YES];
    
    
}
#pragma mark -- 时间选择器delegate
- (void)dateView:(CYFDateView *)dataView didSelectedDate:(NSDate *)date{
    
    NSDate *selectDate =  date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *dateAndTime =  [dateFormatter stringFromDate:selectDate];
    if (dataView.dateTag == 1) {// 开始时间
        //比较时间
        int  result =  [Tool compareOneDay:[dateFormatter dateFromString:dateAndTime] withAnotherDay:[dateFormatter dateFromString:self.endDate]];
        
        if (result == 1) {
            [Tool alertWithMessage:@"起始时间大于结束时间" who:self];
            
            return;
        }
        
        self.benginDate = dateAndTime;
        [self.queryDataHeader setBenginDate:dateAndTime];
        
    }else{ //结束时间
        
        
        //比较时间
        int  result =  [Tool compareOneDay:[dateFormatter dateFromString:dateAndTime] withAnotherDay:[dateFormatter dateFromString:self.benginDate]];
        
        if (result == -1) {

            [Tool alertWithMessage:@"结束时间小于起始时间" who:self];
            
            return;
        }
        
        self.endDate = dateAndTime;
        [self.queryDataHeader setEndDate:dateAndTime];

    }
    
}

#pragma mark - 交易方式
static NSString *xdActionSheetPaymentReuserId = @"xdActionSheetPaymentReuserId";

#pragma mark --交易方式列表
- (XDActionSheet *)paymentTypeSheet{
    
    
    
    _paymentTypeSheet = [[XDActionSheet alloc]initWithFrame:CGRectZero];
    _paymentTypeSheet.delegate = self;
    _paymentTypeSheet.datasource = self;
    [_paymentTypeSheet registerClass:[UITableViewCell class] forCellReuseIdentifier:xdActionSheetPaymentReuserId];
    
    
    [[UIApplication sharedApplication].keyWindow addSubview:_paymentTypeSheet];
    return _paymentTypeSheet;
}

#pragma mark - 交易状态
- (XDActionSheet *)paymentStatuSheet{
        
    
        _paymentStatuSheet = [[XDActionSheet alloc]initWithFrame:CGRectZero];
        _paymentStatuSheet.delegate = self;
        _paymentStatuSheet.datasource = self;
        [_paymentStatuSheet registerClass:[UITableViewCell class] forCellReuseIdentifier:xdActionSheetPaymentReuserId];
        
        
        [[UIApplication sharedApplication].keyWindow addSubview:_paymentStatuSheet];
        return _paymentStatuSheet;
    
}

#pragma mark -- 底部菜单列表代理
- (NSInteger)popoverListView:(XDActionSheet *)sheet numberOfRowsInSection:(NSInteger)section{
    
    if (sheet == _paymentTypeSheet) {
        
        return  self.paymentTypeTitleArray.count ;

    }else{
        return  self.paymentStatuTitleArray.count;

    }
    
}

- (UITableViewCell *)popoverListView:(XDActionSheet *)sheet cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (sheet == _paymentTypeSheet) {//显示交易方式列表数据
        
        if (indexPath.section == 0) {
            UITableViewCell *cell = [sheet dequeueReusablePopoverCellWithIdentifier:xdActionSheetPaymentReuserId];
            
            cell.textLabel.text = self.paymentTypeTitleArray[indexPath.row];

            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.font = KSHSystemFont(13);
            cell.textLabel.textColor = KSHTEXTCOLOR;
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, __I6Height(45)-0.5, [UIScreen mainScreen].bounds.size.width,0.5)];
            line.backgroundColor = KLINECOLOR;
            
            
            [cell addSubview:line];
//            if (indexPath.row < self.paymentTypeTitleArray.count - 2) {
//
//
//            }
//
            return cell;
            
        }else{
            UITableViewCell *cell = [sheet dequeueReusablePopoverCellWithIdentifier:xdActionSheetPaymentReuserId];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.font = KSHSystemFont(13);
            cell.textLabel.textColor = KSHTEXTCOLOR;

            cell.textLabel.text = @"取消";
            return cell;
            
        }
        
        
    }else{//显示交易状态列表数据
        
        if (indexPath.section == 0) {
            UITableViewCell *cell = [sheet dequeueReusablePopoverCellWithIdentifier:xdActionSheetPaymentReuserId];
            
            cell.textLabel.text = self.paymentStatuTitleArray[indexPath.row];

            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.font = KSHSystemFont(13);
            cell.textLabel.textColor = KSHTEXTCOLOR;

            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, __I6Height(45)-0.5, [UIScreen mainScreen].bounds.size.width,0.5)];
            line.backgroundColor = KLINECOLOR;
            
            
            [cell addSubview:line];
            
            return cell;
            
        }else{
            
            UITableViewCell *cell = [sheet dequeueReusablePopoverCellWithIdentifier:xdActionSheetPaymentReuserId];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.font = KSHSystemFont(13);
            cell.textLabel.textColor = KSHTEXTCOLOR;

            cell.textLabel.text = @"取消";
            return cell;
            
        }
        
    }
   
    
}

//====== 切换支付方式======
- (void)popoverListView:(XDActionSheet *)sheet didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (sheet == _paymentTypeSheet) {
        
        if (indexPath.section == 0) {//切换交易方式
            
            self.paymentTypeStr = self.paymentTypeTitleArray[indexPath.row];
            [self.tradingQuryTableView  reloadData];
            
        }
    }else{
        
        if (indexPath.section == 0) {//切换交易zhuangtai1
            
            self.paymentStatu = self.paymentStatuTitleArray[indexPath.row];
            [self.tradingQuryTableView  reloadData];
            
        }
       
    }
    
    
    
}





#pragma mark - 查询 开始查询交易记录
- (void)changeFooter:(SHChangeFooter *)footer changeCardBtn:(UIButton *)sender{
    
    
    NSNumber *payment_type ;
    NSNumber *status ;
    if ([self.paymentTypeStr isEqualToString:@"杉德收款"]) {
        
        payment_type = @(4);
        
    }else if([self.paymentTypeStr isEqualToString:@"微信扫码支付"]){
        
        payment_type = @(10);
        
    }else if([self.paymentTypeStr isEqualToString:@"支付宝扫码支付"]){
        
        payment_type = @(20);
        
    }else if([self.paymentTypeStr isEqualToString:@"我是瀚付"]){
        
        payment_type = @(self.paymentType);
        
    }

    if ([self.paymentStatu isEqualToString:@"支付成功"]){
        
        status = @(1);
        
    }else if([self.paymentStatu isEqualToString:@"等待支付"]){
        
        status = @(5);
        
    }else if([self.paymentStatu isEqualToString:@"支付失败"]){
        
        status = @(10);
    }
    
    
    NSDictionary *params;
   
    
    if (payment_type != nil && status != nil ) {
        params = @{@"date_start":self.benginDate,@"date_end":self.endDate,@"payment_type":payment_type,@"status":status};
    }

    if (payment_type != nil && status == nil) {
        params = @{@"date_start":self.benginDate,@"date_end":self.endDate,@"payment_type":payment_type};
    }
    
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:KUser_get_payment_order httpMethod:@"GET" params:params data:nil complection:^(id result) {
        
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            if([result[@"data"] count] == 0){
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [self.view makeToast:@"未查到任何交易数据" duration:2.f position:@"center"];

            }else{
                TradeNotesViewController *tradeNotes =    [[TradeNotesViewController alloc]init];
                tradeNotes.dataArr = result[@"data"];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                
                [self.navigationController pushViewController:tradeNotes animated:YES];
            }
            
            
        }else{
            
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
    } failure:^(id erro) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
  
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end

    
    
